<?php
function mobile_service_user_info($parameters)
{
    $role = $parameters['role'];
    $id = $parameters['uid'];
    switch ($role) {
        case "农户":
            $results = db_select('farmer', 'R')
                -> fields('R')
                -> condition('R.farmer_id',$id, '=')
                -> execute();
            if(!empty($results)){
                foreach ($results as $result) {
                    $user_info[] = $result;
                }
            }
            $object_user_info = (object)["data"=>$user_info];
            return $object_user_info;
            break;
        case "经销商":
            $results = db_select('distributor', 'P')
                -> fields('P')
                -> condition('P.distributor_id',$id, '=')
                -> execute();
            if(!empty($results)){
                foreach ($results as $result) {
                    $user_info[] = $result;
                }
            }
            $object_user_info = (object)["data"=>$user_info];
            return $object_user_info;
            break;
        case "employee":
            $results = db_select('employee', 'P')
                -> fields('P')
                -> condition('P.employee_id',$id, '=')
                -> execute();
            if(!empty($results)){
                foreach ($results as $result) {
                    $user_info[] = $result;
                }
            }
            $object_user_info = (object)["data"=>$user_info];
            return $object_user_info;
            break;
    }
}
function mobile_service_get_select_data_info($parameters)
{
    $action = $parameters['action'];
    switch ($action) {
        case "get_city":
            $init_city ="";
            if(isset($parameters['province'])&&$parameters['province'] === "2"){
                $init_city = 3925;
                $city_options[] =['id'=>'3925','label'=>'北京市'];
            }else if(isset($parameters['province'])&&$parameters['province'] === "19"){
                $init_city = 3926;
                $city_options[] =['id'=>'3926','label'=>'天津市'];
            }else if(isset($parameters['province'])&&$parameters['province'] ==="857" ){
                $init_city = 3927;
                $city_options[] =['id'=>'3927','label'=>'上海市'];
            }else if(isset($parameters['province'])&&$parameters['province'] ==="2459" ){
                $init_city = 3928;
                $city_options[] =['id'=>'3928','label'=>'重庆市'];
            }else{
                $city_options[] = ['id'=>"",'label'=>"请选市"];
            }

            $selected_province = $parameters['province'];

            $result_records = db_query("SELECT addr_id, name from {addr} where geo_level = 2 and pid = :pid", array(':pid' => $selected_province));
            foreach ($result_records as $record) {
                $city_options[] =['id'=>$record->addr_id,'label'=>$record->name] ;
            }
            if(!empty($init_city)){
                $selected_city = $init_city;
                $county_options[] = ['id'=>"",'label'=>"请选县"];
                $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 3 and pid = :pid", array(':pid' => $selected_city));
                foreach ($result_records as $record) {
                    $county_options[] =['id'=>$record->addr_id,'label'=>$record->name] ;
                }
                $object_result = (object)["data" => $city_options,"extra" => $county_options];
                return $object_result;
            }
            $object_result = (object)["data" => $city_options];
            return $object_result;
        case "get_county":
            $selected_city = $parameters['city'];
            $county_options[] = ['id'=>"",'label'=>"请选县"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 3 and pid = :pid", array(':pid' => $selected_city));
            foreach ($result_records as $record) {
                $county_options[] =['id'=>$record->addr_id,'label'=>$record->name] ;
            }
            $object_result = (object)["data" => $county_options];
            return $object_result;
        case "get_town":
            $selected_county = $parameters['county'];
            $town_options[] = ['id'=>"",'label'=>"请选乡镇"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 4 and pid = :pid", array(':pid' => $selected_county));
            foreach ($result_records as $record) {
                $town_options[] =['id'=>$record->addr_id,'label'=>$record->name] ;
            }
            $object_result = ["data" => $town_options];
            if(isset($parameters['farmer'])&& $parameters['farmer']=="get"){
                $query = db_select('addr', 'A');//地址表
                $query->join('farmer', 'D', 'A.addr_id=D.addr_id');
                $query->fields('D');
                //地址级别查询
                $query->condition('A.county_id', $parameters['county'], '=');
                $result_records = $query->execute();
                $farmer_options[] = ['id'=>"",'label'=>"请选农户"];
                foreach ($result_records as $r) {
                    $farmer_options[] =['id'=>$r->farmer_id,'label'=>$r->farmer_name] ;
                }
                $object_result["farmer"] =  $farmer_options;
            }
            return $object_result;
        case "get_village":
            $selected_town = $parameters['town'];
            $village_options[] = ['id'=>"",'label'=>"请选村落"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 5 and pid = :pid", array(':pid' => $selected_town));
            foreach ($result_records as $record) {
                $village_options[] =['id'=>$record->addr_id,'label'=>$record->name] ;
            }
            $object_result = ["data" => $village_options];

            if(isset($parameters['farmer'])&& $parameters['farmer']=="get"){
                $query = db_select('addr', 'A');//地址表
                $query->join('farmer', 'D', 'A.addr_id=D.addr_id');
                $query->fields('D');
                //地址级别查询
                $query->condition('A.town_id', $parameters['town'], '=');
                $result_records = $query->execute();
                $farmer_options[] = ['id'=>"",'label'=>"请选农户"];
                foreach ($result_records as $r) {
                    $farmer_options[] =['id'=>$r->farmer_id,'label'=>$r->farmer_name] ;
                }
                $object_result["farmer"] =  $farmer_options;
            }
            return (object)$object_result;
        case "get_product":
            $product_options[] = ['id'=>"",'label'=>"--化肥名称--"];
            $result = db_select('product_sku', 'D')
                ->condition('D.brand_id', '1')
                ->fields('D', array('product_sku_id', 'variety_name', 'proportion', 'SM'))
                ->execute();
            foreach ($result as $record) {
                $product_options[] =['id'=>$record->product_sku_id,'label'=>$record->variety_name . $record->proportion . $record->SM] ;
            }
            $object_result = ["data" => $product_options];
            return $object_result;
        case "get_plant":
            $plants = db_select('plant', 'P')
                ->fields('P', array('plant_id', 'plant_name'))
                ->execute();
            $plant_options[] = ['id'=>"",'label'=>"--作物名称--"];
            foreach ($plants as $record) {
                $plant_options[] =['id'=>$record->plant_id,'label'=>$record->plant_name] ;
            }
            $object_result = ["data" => $plant_options];
            return $object_result;
        case "get_init_addr_id":
            $addr_record = db_select('addr', 'a')
                ->fields('a')
                ->condition('addr_id', $parameters['init_addr_id'], '=')
                ->execute()
                ->fetchObject();
            $init_addr['init_province']= $addr_record->province_id;
            $selected_province = $init_addr['init_province'];

            $city_options[] = ['id'=>"",'label'=>"请选市"];
            $result_records = db_query("SELECT addr_id, name from {addr} where geo_level = 2 and pid = :pid", array(':pid' => $selected_province));
            foreach ($result_records as $record) {
                $city_options[] = ['id'=>$record->addr_id,'label'=>$record->name];
            }
            $init_addr['init_city']= $addr_record->city_id;
            $selected_city = $init_addr['init_city'];

            $county_options[] = ['id'=>"",'label'=>"请选县"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 3 and pid = :pid", array(':pid' => $selected_city));
            foreach ($result_records as $record) {
                $county_options[] = ['id'=>$record->addr_id,'label'=>$record->name];
            }
            $init_addr['init_county']= $addr_record->county_id;

            $selected_county = $init_addr['init_county'];
            $town_options[] = ['id'=>"",'label'=>"请选乡镇"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 4 and pid = :pid", array(':pid' => $selected_county));
            foreach ($result_records as $record) {
                $town_options[] = ['id'=>$record->addr_id,'label'=>$record->name];
            }
            $init_addr['init_town']= $addr_record->town_id;

            $selected_town = $init_addr['init_town'];
            $village_options[] = ['id'=>"",'label'=>"请选村落"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 5 and pid = :pid", array(':pid' => $selected_town));
            foreach ($result_records as $record) {
                $village_options[] = ['id'=>$record->addr_id,'label'=>$record->name];
            }
            $init_addr['init_village']= $addr_record->village_id;
            $object_result = (object)["data" => $init_addr,
                "city" => $city_options,
                "county" => $county_options,
                "town" => $town_options,
                "village" => $village_options,
            ];
            return $object_result;
        case "get_years":
            $years = db_select('years','y')
                ->fields('y')
                ->execute();
            $year_options[0] = '年份';
            foreach ($years as $year) {
                $year_options[$year->year] = $year->year;
            }
            $object_result = (object)["data" => $year_options];
            return $object_result;
        case 'get_brand':
            $brand_record = db_select('brand','B')
                ->fields('B')
                ->execute();
            $brand_options[] = ['id'=>'','label'=>'--品牌--'];

            foreach($brand_record as $brand){
                $brand_options[] = ['id'=>$brand->brand_id,'label'=>$brand->brand_name];
            }
            $object_result = ["data" => $brand_options];
            return $object_result;
        case 'get_fertilizer':
            $fertilizer_record =db_select('fertilizer','f')
                ->fields('f')
                ->execute();
            $fertilizer_options[] =['id'=>'','label'=>'--肥料类别--'] ;
            foreach($fertilizer_record as $fertilizer){
                $fertilizer_options[] =['id'=>$fertilizer->fertilizer_id,'label'=>$fertilizer->fertilizer_name] ;
            }
            $object_result = ["data" => $fertilizer_options];
            return $object_result;
        case 'get_sacfvariety':
            $sacfvariety_options[] =['id'=>'','label'=>'--化肥名称--'] ;
            $result = db_select('product_sku', 'D')
                ->condition('D.brand_id','1')
                ->fields('D', array('product_sku_id', 'variety_name','proportion','SM'))
                ->execute();
            foreach ($result as $record) {
                $sacfvariety_options[] =['id'=>$record->product_sku_id,'label'=>$record->variety_name.$record->proportion.$record->SM] ;
            }
            $object_result = ["data" => $sacfvariety_options];
            return $object_result;
        case 'get_sales_type':
            $sales_type = db_select('sales_type','st')
                ->fields('st')
                ->execute();
            $sales_type_options[] =['id'=>'','label'=>'--销售类型--'] ;
            foreach($sales_type as $type){
                $sales_type_options[] =['id'=>$type->sales_type_id,'label'=>$type->sales_type_name] ;
            }
            $object_result = ["data" => $sales_type_options];
            return $object_result;
        case 'get_addr_distributor':
            $parameters['province'] = isset($parameters['province']) && !empty($parameters['province'])?$parameters['province']:'';
            $parameters['city'] = isset($parameters['city']) && !empty($parameters['city'])?$parameters['city']:'';
            $parameters['county'] = isset($parameters['county']) && !empty($parameters['county'])?$parameters['county']:'';
            if(empty($parameters['province'])&&empty($parameters['city'])&&empty($parameters['county']) ){
                if(!empty($parameters['addr_id']))
                $addr_record = db_select('addr', 'a')
                    ->fields('a')
                    ->condition('addr_id', $parameters['addr_id'], '=')
                    ->execute()
                    ->fetchObject();
                $parameters['province']= $addr_record->province_id;
                $parameters['city']= $addr_record->city_id;
                $parameters['county']= $addr_record->county_id;
            }
            $query = db_select('addr', 'A');//地址表
            $query->join('distributor', 'D', 'A.addr_id=D.addr_id');//内连接 成一个表
            $query->fields('D');
            //地址级别查询
           if (!empty($parameters['county'])) {//县级
                $query->condition('A.county_id', $parameters['county'], '=');
            } elseif (!empty($parameters['city'])) {//市级
                $query->condition('A.city_id', $parameters['city'], '=');
            } elseif (!empty($parameters['province'])) {//省级
                $query->condition('A.province_id', $parameters['province'], '=');
           }
            $result_records = $query->execute();
            $distributor_options[0] = '--请选经销商--';
            foreach ($result_records as $r) {
                $distributor_options[$r->distributor_id] = $r->distributor_name;
            }
            $object_result = (object)["data" => $distributor_options];
            return $object_result;
        case 'get_addr_from_farmer':
            $record = db_select('farmer','d')
                ->condition('d.farmer_id',$parameters['init_farmer_id'])
                ->fields('d')
                ->execute()
                -> fetchObject();
//            $object_result = (object)["data" => $record->addr_id];
            $addr_record = db_select('addr', 'a')
                ->fields('a')
                ->condition('addr_id', $record->addr_id, '=')
                ->execute()
                ->fetchObject();
            $parameters['county']= $addr_record->county_id;
            $parameters['town']= $addr_record->town_id;
            $parameters['village']= $addr_record->village_id;

            $init_addr['init_province']= $addr_record->province_id;
            $selected_province = $init_addr['init_province'];

            $city_options[] = ['id'=>"",'label'=>"请选市"];
            $result_records = db_query("SELECT addr_id, name from {addr} where geo_level = 2 and pid = :pid", array(':pid' => $selected_province));
            foreach ($result_records as $record) {
                $city_options[] = ['id'=>$record->addr_id,'label'=>$record->name];
            }
            $init_addr['init_city']= $addr_record->city_id;
            $selected_city = $init_addr['init_city'];

            $county_options[] = ['id'=>"",'label'=>"请选县"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 3 and pid = :pid", array(':pid' => $selected_city));
            foreach ($result_records as $record) {
                $county_options[] = ['id'=>$record->addr_id,'label'=>$record->name];
            }
            $init_addr['init_county']= $addr_record->county_id;
            $selected_county = $init_addr['init_county'];

            $town_options[] = ['id'=>"",'label'=>"请选乡镇"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 4 and pid = :pid", array(':pid' => $selected_county));
            foreach ($result_records as $record) {
                $town_options[] = ['id'=>$record->addr_id,'label'=>$record->name];
            }
            $init_addr['init_town']= $addr_record->town_id;
            $selected_town = $init_addr['init_town'];

            $village_options[] = ['id'=>"",'label'=>"请选村落"];
            $result_records = db_query("SELECT addr_id, name from {addr} where  geo_level = 5 and pid = :pid", array(':pid' => $selected_town));
            foreach ($result_records as $record) {
                $village_options[] = ['id'=>$record->addr_id,'label'=>$record->name];
            }
            $init_addr['init_village']= $addr_record->village_id;
//            $init_addr['init_farmer']= $parameters['init_farmer_id'];
            $query = db_select('addr', 'A');//地址表
            $query->join('farmer', 'D', 'A.addr_id=D.addr_id');//内连接 成一个表
            $query->fields('D');
            //地址级别查询
            if (!empty($parameters['village'])) {//村级
                $query->condition('A.village_id', $parameters['village'], '=');
            } elseif (!empty($parameters['town'])) {//乡镇级
                $query->condition('A.town_id', $parameters['town'], '=');
            } elseif (!empty($parameters['county'])) {//县级
                $query->condition('A.county_id', $parameters['county'], '=');
            }
            $result_records = $query->execute();

//            $farmer_options[0] = '请选农户';
            $farmer_options[] = ['id'=>"",'label'=>"请选农户"];
            foreach ($result_records as $r) {
//                $farmer_options[$r->farmer_id] = $r->farmer_name;
                $farmer_options[] = ['id'=>$r->farmer_id,'label'=>$r->farmer_name];
            }
            $object_result = (object)["data" => $init_addr,
                "city" => $city_options,
                "county" => $county_options,
                "town" => $town_options,
                "village" => $village_options,
                "farmer" => $farmer_options
            ];
            return $object_result;
        case 'get_addr_farmer':
            $parameters['county'] = isset($parameters['county']) && !empty($parameters['county'])?$parameters['county']:'';
            $parameters['town'] = isset($parameters['town']) && !empty($parameters['town'])?$parameters['town']:'';
            $parameters['village'] = isset($parameters['village']) && !empty($parameters['village'])?$parameters['village']:'';
            if(empty($parameters['province'])&&empty($parameters['city'])&&empty($parameters['county']) ){
                if(!empty($parameters['init_addr_id']))
                    $addr_record = db_select('addr', 'a')
                        ->fields('a')
                        ->condition('addr_id', $parameters['init_addr_id'], '=')
                        ->execute()
                        ->fetchObject();
                $parameters['county']= $addr_record->county_id;
                $parameters['town']= $addr_record->town_id;
                $parameters['village']= $addr_record->village_id;
            }
            $query = db_select('addr', 'A');//地址表
            $query->join('farmer', 'D', 'A.addr_id=D.addr_id');//内连接 成一个表
            $query->fields('D');
            //地址级别查询
            if (!empty($parameters['village'])) {//村级
                $query->condition('A.village_id', $parameters['village'], '=');
            } elseif (!empty($parameters['town'])) {//乡镇级
                $query->condition('A.town_id', $parameters['town'], '=');
            } elseif (!empty($parameters['county'])) {//县级
                $query->condition('A.county_id', $parameters['county'], '=');
            }
            $result_records = $query->execute();
            $farmer_options[] = ['id'=>"",'label'=>"请选农户"];
            foreach ($result_records as $r) {
                $farmer_options[] = ['id'=>$r->farmer_id,'label'=>$r->farmer_name];
            }
            $object_result = (object)["data" => $farmer_options];
            return $object_result;
        case 'get_distributor_all':
            $distributor_options[] = ['id'=>'','label'=>'请选经销商'];
            $query = db_select('distributor', 'D1');
            $query->leftJoin('distributor', 'D2', 'D2.parent_distributor_id = D1.distributor_id');
            $query->leftJoin('distributor', 'D3', 'D3.parent_distributor_id = D2.distributor_id');
            $query->leftJoin('distributor', 'D4', 'D4.parent_distributor_id = D3.distributor_id');
            $query->leftJoin('distributor', 'D5', 'D5.parent_distributor_id = D4.distributor_id');
            $query->leftJoin('distributor', 'D6', 'D6.parent_distributor_id = D5.distributor_id');
            $query->condition('D1.level',1, '=');
            $query->addField('D1', 'distributor_id ','distributor_id');
            $query->addField('D1', 'distributor_name','distributor_name');

            $query->addField('D2', 'distributor_id ','one_distributor_id');
            $query->addField('D2', 'distributor_name','one_distributor_name');

            $query->addField('D3', 'distributor_id','two_distributor_id');
            $query->addField('D3', 'distributor_name','two_distributor_name');

            $query->addField('D4', 'distributor_id','three_distributor_id');
            $query->addField('D4', 'distributor_name','three_distributor_name');

            $query->addField('D5', 'distributor_id','four_distributor_id');
            $query->addField('D5', 'distributor_name','four_distributor_name');

            $query->addField('D6', 'distributor_id','five_distributor_id');
            $query->addField('D6', 'distributor_name','five_distributor_name');
            $results = $query->execute()->fetchAll();
//    print_r($results);die;
////    print_r($results);die;
            $i = 1;
            foreach($results as $key => $value){
                if(!empty($value->five_distributor_id)){
                    $distributor_options[] = ['id'=>$value->distributor_id,'label'=>"┅　".$value->distributor_name];
                    $distributor_options[] = ['id'=>$value->one_distributor_id,'label'=>"　┅　".$value->one_distributor_name];
                    $distributor_options[] = ['id'=>$value->two_distributor_id,'label'=>"　　┅　".$value->two_distributor_name];
                    $distributor_options[] = ['id'=>$value->three_distributor_id,'label'=>"　　　┅　".$value->three_distributor_name];
                    $distributor_options[] = ['id'=>$value->four_distributor_id,'label'=>"　　　　┅　".$value->four_distributor_name];
                    $distributor_options[] = ['id'=>$value->five_distributor_id,'label'=>"　　　　　┅　".$value->five_distributor_name];
                }else if(!empty($value->four_distributor_id)){
                    $distributor_options[] = ['id'=>$value->distributor_id,'label'=>"┅　".$value->distributor_name];
                    $distributor_options[] = ['id'=>$value->one_distributor_id,'label'=>"　┅　".$value->one_distributor_name];
                    $distributor_options[] = ['id'=>$value->two_distributor_id,'label'=>"　　┅　".$value->two_distributor_name];
                    $distributor_options[] = ['id'=>$value->three_distributor_id,'label'=>"　　　┅　".$value->three_distributor_name];
                    $distributor_options[] = ['id'=>$value->four_distributor_id,'label'=>"　　　　┅　".$value->four_distributor_name];
                }else if(!empty($value->three_distributor_id)){
                    $distributor_options[] = ['id'=>$value->distributor_id,'label'=>"┅　".$value->distributor_name];
                    $distributor_options[] = ['id'=>$value->one_distributor_id,'label'=>"　┅　".$value->one_distributor_name];
                    $distributor_options[] = ['id'=>$value->two_distributor_id,'label'=>"　　┅　".$value->two_distributor_name];
                    $distributor_options[] = ['id'=>$value->three_distributor_id,'label'=>"　　　┅　".$value->three_distributor_name];
                }else if(!empty($value->two_distributor_id)){
                    $distributor_options[] = ['id'=>$value->distributor_id,'label'=>"┅　".$value->distributor_name];
                    $distributor_options[] = ['id'=>$value->one_distributor_id,'label'=>"　┅　".$value->one_distributor_name];
                    $distributor_options[] = ['id'=>$value->two_distributor_id,'label'=>"　　┅　".$value->two_distributor_name];
                }else if(!empty($value->one_distributor_id)){
                    $distributor_options[] = ['id'=>$value->distributor_id,'label'=>"┅　".$value->distributor_name];
                    $distributor_options[] = ['id'=>$value->one_distributor_id,'label'=>"　┅　".$value->one_distributor_name];
                }else{
                    $distributor_options[] = ['id'=>$value->distributor_id,'label'=>"┅　".$value->distributor_name];
                }
            }
            $object_result = ["data" => $distributor_options];
            return $object_result;
        case 'get_distributor':
        global $user;
        $user_info = _get_user_info($user->uid);
        $distributor_options[0] = '经销商';

        if (isset($user_info['manager']) && $user_info['manager'] === '0') {//经理
            $query = db_select('employee', 'e1');
            $query->join('employee', 'e2', 'e2.manager_uid = e1.employee_id');
            $query->condition('e1.department_id', 1, '=');
            $query->condition('e2.department_id', 1, '=');
            $query->condition('e1.employee_id', $user->uid, '=');
            $query->join('salesman_distributor_map', 'sd', 'sd.salesman_id=e2.employee_id or sd.salesman_id ='.$user->uid);
            $query->join('distributor', 'd', 'd.distributor_id = sd.distributor_id');
            $query->fields('d', array('distributor_id', 'distributor_name'));
            $result_records = $query->execute();
        } else {//业务员
            $query = db_select('employee', 'e1');
            $query->condition('e1.department_id', 1, '=');
            $query->condition('e1.employee_id', $user->uid, '=');
            $query->join('salesman_distributor_map', 'sd', 'sd.salesman_id=e1.employee_id or sd.salesman_id ='.$user->uid);
            $query->join('distributor', 'd', 'd.distributor_id = sd.distributor_id');
            $query->fields('d', array('distributor_id', 'distributor_name'));
            $result_records = $query->execute();
        }
        foreach ($result_records as $r) {
            $distributor_options[$r->distributor_id] = $r->distributor_name;
        }
        $object_result = (object)["data" => $distributor_options];
        return $object_result;
        case 'get_monthly_report':
            $monthly_report = array();
            if(isset($parameters['distributor'])&&!empty($parameters['distributor']) && !empty($parameters['year'] )&& !empty($parameters['month'])){
                $sql_sum = 'select SUM(this_month_instock) as this_month_instock, sum(this_month_stock) as this_month_stock,sum(this_month_sales)as this_month_sales from stock_info
             where distributor_id ='.$parameters['distributor'].' and year = '.$parameters['year'].' and month='.$parameters['month'];
                $results = db_query($sql_sum);
                foreach($results as $result) {
                    $monthly_report['this_month_instock'] = $result->this_month_instock;
                    $monthly_report['this_month_sales'] = $result->this_month_sales;
                    $monthly_report['this_month_stock'] = $result->this_month_stock;
                }
                $sql_sum_last_year = 'select SUM(this_month_instock) as this_month_instock, sum(this_month_stock) as this_month_stock,sum(this_month_sales)as this_month_sales from stock_info
             where distributor_id ='.$parameters['distributor'].' and year = '.($parameters['year']-1).' and month='.$parameters['month'];
                $results = db_query($sql_sum_last_year);
                foreach($results as $result) {
                    $monthly_report['last_year_this_month_instock'] = $result->this_month_instock;
                    $monthly_report['last_year_this_month_sales'] = $result->this_month_sales;
                    $monthly_report['last_year_this_month_stock'] = $result->this_month_stock;
                }
                $distributor_info = _get_user_info($parameters['distributor']);
                if($distributor_info['inorout'] === '关内'){
                    $sql_accumulative = 'select sum(this_month_sales)as this_month_sales from stock_info
             where distributor_id ='.$parameters['distributor'].' and year = '.$parameters['year'].' and month>=1 and month<='.$parameters['month'];
                    $results = db_query($sql_accumulative);
                    foreach($results as $result) {
                        $monthly_report['accumulative_total_sales'] = $result->this_month_sales;
                    }
                    $last_year_sql_accumulative = 'select sum(this_month_sales)as this_month_sales from stock_info
             where distributor_id ='.$parameters['distributor'].' and year = '.($parameters['year']-1).' and month>=1 and month<='.$parameters['month'];
                    $results = db_query($last_year_sql_accumulative);
                    foreach($results as $result) {
                        $monthly_report['last_year_accumulative_total_sales'] = $result->this_month_sales;
                    }
                }else if($distributor_info['inorout'] === '关外'){
                    if($parameters['month']<9) {
                        $date_max = $parameters['year'] . '-0' . $parameters['month'] . '-00';
                        $date_min = ($parameters['year'] - 1) . '-09' . '-00';
                        $last_year_date_max = ($parameters['year']-1).'-0'.$parameters['month'].'-00';
                        $last_year_date_min = ($parameters['year']-2).'-09'.'-00';
                    }else{
                        if($parameters['month']<10){
                            $date_max = $parameters['year'] . '-0' . $parameters['month'] . '-00';
                            $last_year_date_max = ($parameters['year']-1).'-0'.$parameters['month'].'-00';
                        }else{
                            $date_max = $parameters['year'] . '-' . $parameters['month'] . '-00';
                            $last_year_date_max = ($parameters['year']-1).'-'.$parameters['month'].'-00';
                        }
                        $date_min = ($parameters['year']) . '-09' . '-00';
                        $last_year_date_min = ($parameters['year']-1).'-09'.'-00';
                    }
                    unset($results);
                    unset($sql_accumulative);
                    $sql_accumulative = 'select sum(t.this_month_sales) as this_month_sales from
                 (select date_format(concat(year,\'-\',month,\'-00\'),\'%Y-%m-%d\') as date,distributor_id,this_month_sales from stock_info)t where
                        t.date >=\''.$date_min.'\' and t.date<=\''.$date_max.'\' and t.distributor_id =\''.$parameters['distributor'].'\'';
                    $results = db_query($sql_accumulative);
                    foreach($results as $result) {
                        $monthly_report['accumulative_total_sales'] = $result->this_month_sales;
                    }
                    unset($results);
                    unset($sql_accumulative);
                    $sql_accumulative = 'select sum(t.this_month_sales) as this_month_sales from
                 (select date_format(concat(year,\'-\',month,\'-00\'),\'%Y-%m-%d\') as date,distributor_id,this_month_sales from stock_info)t where
                  t.date >=\''.$last_year_date_min.'\' and t.date<=\''.$last_year_date_max.'\' and t.distributor_id =\''.$parameters['distributor'].'\'';
                    $results = db_query($sql_accumulative);
                    foreach($results as $result) {
                        $monthly_report['last_year_accumulative_total_sales'] = $result->this_month_sales;
                    }
                }
            }
            $object_result = (object)["data" => $monthly_report];
            return $object_result;
            break;
        case 'get_employee':
            global $user;
            if($parameters['type'] == 'search'){
                $employee_options['default'] = '';
            }
            $employee_options[] = ['id'=>'','label'=>'请选销售员'];
            $employee_options[] = ['id'=>$user->uid,'label'=>'本人'];
            $query = db_select('employee', 'e1');
            $query->leftJoin('employee', 'e2', 'e2.manager_uid = e1.employee_id');
            $query->leftJoin('employee', 'e3', 'e3.manager_uid = e2.employee_id');
            $query->leftJoin('employee', 'e4', 'e4.manager_uid = e3.employee_id');
            $query->leftJoin('employee', 'e5', 'e5.manager_uid = e4.employee_id');
            $query->leftJoin('employee', 'e6', 'e6.manager_uid = e5.employee_id');
            $query->condition('e1.employee_id', $user->uid, '=');
            $query->condition('e2.department_id', 1, '=');
            $query->addField('e1', 'employee_id ','employee_id');
            $query->addField('e1', 'employee_name','employee_name');

            $query->addField('e2', 'employee_id ','one_employee_id');
            $query->addField('e2', 'employee_name','one_employee_name');
            $query->addField('e2', 'manager_uid','one_manager_uid');

            $query->addField('e3', 'employee_id','two_employee_id');
            $query->addField('e3', 'employee_name','two_employee_name');
            $query->addField('e3', 'manager_uid','two_manager_uid');

            $query->addField('e4', 'employee_id','three_employee_id');
            $query->addField('e4', 'employee_name','three_employee_name');
            $query->addField('e4', 'manager_uid','three_manager_uid');

            $query->addField('e5', 'employee_id','four_employee_id');
            $query->addField('e5', 'employee_name','four_employee_name');
            $query->addField('e5', 'manager_uid','four_manager_uid');

            $query->addField('e6', 'employee_id','five_employee_id');
            $query->addField('e6', 'employee_name','five_employee_name');
            $query->addField('e6', 'manager_uid','five_manager_uid');
            $results = $query->execute()->fetchAll();
            foreach($results as $key => $value){
                if(!empty($value->five_employee_id)){
                    $employee_options[] = ['id'=>$value->one_employee_id,'label'=>"┅　".$value->one_employee_name];
                    $employee_options[] = ['id'=>$value->two_employee_id,'label'=>"　┅　".$value->two_employee_name];
                    $employee_options[] = ['id'=>$value->three_employee_id,'label'=>"　　┅　".$value->three_employee_name];
                    $employee_options[] = ['id'=>$value->four_employee_id,'label'=>"　　　┅　".$value->four_employee_name];
                    $employee_options[] = ['id'=>$value->five_employee_id,'label'=>"　　　　┅　".$value->five_employee_name];
                }else if(!empty($value->four_employee_id)){
                    $employee_options[] = ['id'=>$value->one_employee_id,'label'=>"┅　".$value->one_employee_name];
                    $employee_options[] = ['id'=>$value->two_employee_id,'label'=>"　┅　".$value->two_employee_name];
                    $employee_options[] = ['id'=>$value->three_employee_id,'label'=>"　　┅　".$value->three_employee_name];
                    $employee_options[] = ['id'=>$value->four_employee_id,'label'=>"　　　┅　".$value->four_employee_name];
                }else if(!empty($value->three_employee_id)){
                    $employee_options[] = ['id'=>$value->one_employee_id,'label'=>"┅　".$value->one_employee_name];
                    $employee_options[] = ['id'=>$value->two_employee_id,'label'=>"　┅　".$value->two_employee_name];
                    $employee_options[] = ['id'=>$value->three_employee_id,'label'=>"　　┅　".$value->three_employee_name];
                }else if(!empty($value->two_employee_id)){
                    $employee_options[] = ['id'=>$value->one_employee_id,'label'=>"┅　".$value->one_employee_name];
                    $employee_options[] = ['id'=>$value->two_employee_id,'label'=>"　┅　".$value->two_employee_name];
                }else if(!empty($value->one_employee_id)){
                    $employee_options[] = ['id'=>$value->one_employee_id,'label'=>"┅　".$value->one_employee_name];
                }
            }
            $object_result = ["data" => $employee_options];
            return $object_result;
        case 'get_department_employee':
            global $user;
            $employee_options[] = ['id'=>"",'label'=>"--请选员工--"];
            $query = db_select('employee', 'e')->fields('e',array('employee_id','employee_name'));
            $query->leftJoin('department', 'd', 'e.department_id = d.department_id');
            if(!empty($parameters['department'])){
                $query->condition('d.department_name', '%' . db_like($parameters['department']) . '%', 'LIKE');
            }
            $result_records = $query->execute();
            foreach ($result_records as $r) {
                $employee_options[] = ['id'=>$r->employee_id,'label'=>$r->employee_name];
            }
            $object_result = ["data" => $employee_options];
            return $object_result;
        case "get_juris":
            $result_records = db_select('juris','j')
                ->fields('j',array('juris_id','juris_name'))
                ->execute();
            $distributor_options[0] = '--请选区域--';
            foreach ($result_records as $r) {
                $distributor_options[$r->juris_id] = $r->juris_name;
            }
            $object_result = (object)["data" => $distributor_options];
            return $object_result;
            break;
        case "get_manager":
            $result_records = db_select('employee','j')
                ->fields('j',array('employee_id','employee_name'))
                ->condition('j.department_id', $parameters['department'], '=')
                ->execute();
            $employee_options[0] = '--请选经理--';
            foreach ($result_records as $r) {
                $employee_options[$r->employee_id] = $r->employee_name;
            }
            $object_result = (object)["data" => $employee_options];
            return $object_result;
            break;
        case "get_fertilizer_recomment":
            $query = db_select($parameters['tableName'], 'F');
            $query->join('product_sku','p','F.fertilizer_id = p.product_sku_id');
            $query->condition('F.' .$parameters['field'], $parameters['id']);
            $query->fields('p',array('variety_name','proportion','SM'));
            $query->fields('F');
            $fertilizationInfo = $query->execute()->fetchAll();
            return $fertilizationInfo;
            break;
        case 'get_experts':
            $experts_record = db_select('experts','B')
                ->fields('B')
                ->execute();
            $experts_options[] = ['id'=>'','label'=>'--专家--'];

            foreach($experts_record as $brand){
                $experts_options[] = ['id'=>$brand->experts_id,'label'=>$brand->experts_name];
            }
            $object_result = ["data" => $experts_options];
            return $object_result;
        break;
    }
}

/**
 * 删除信息
 */


function mobile_service_delete_info($parameters){
    global $user;
    if(empty($parameters) && !isset($parameters)){
        return ['success'=>0,'msgNum'=>1];//参数为空
    }
    $transaction = db_transaction();
    try {
        $tableName = $parameters['tableName'];

        switch($parameters['tableName']){
            case "fertilizer_case_extra":
                $parameters['tableName'] = 'fertilizer_case';
                break;
            case "fertilization_info":
                $parameters['tableName'] = 'plant_info';
                break;
            case "fertilizer_recommendation":
                $parameters['tableName'] = 'soil_info';
                break;
        }
        if(!user_access('delete all '.$parameters['tableName'])){
            if(!user_access('delete own '.$parameters['tableName'])){
                return ['success' => 0, 'errorNum' => 3];//无权限
            }else{
                $parameters['tableName'] =$tableName;
                    $record = table_load($parameters['tableName'], $parameters['columnName'], $parameters['id']);
                    if (!empty($record)) {
                        if($record->author_uid != $user->uid){
                            return ['success' => 0, 'errorNum' => 4];//无权限
                        }
                    }else {
                        return ['success' => 0, 'errorNum' => 2];//
                    }
            }
        }
        $parameters['tableName'] =$tableName;
        db_delete($parameters['tableName'])
            ->condition($parameters['columnName'],$parameters['id'])
            ->execute();
        return ['success' => 1, 'errorNum' => 0];

    }catch (Exception $e) {
        $transaction->rollback();
        return ['success'=>0,'errorNum'=>2];//删除失败
    }
}
function mobile_anonymous_access(){
    return true;
}
